package com.ctshk.rpc.finance.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.rpc.finance.base.CustomBaseMapper;
import com.ctshk.rpc.finance.dto.onaccount.OnAccountDetailListDTO;
import com.ctshk.rpc.finance.dto.onaccount.OnAccountUsageCountDTO;
import com.ctshk.rpc.finance.entity.FinanceOnAccountUsageRecord;
import com.ctshk.rpc.finance.req.onaccount.OnAccountDetailListReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 挂账使用记录 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-30
 */
public interface FinanceOnAccountUsageRecordMapper extends CustomBaseMapper<FinanceOnAccountUsageRecord> {

    /**
     * 查询审批中的金额
     * @param accountId
     * @param currencyId
     * @return
     */
    @Select("SELECT SUM(a.total_amount) AS lockAmount \n" +
            "FROM t_finance_on_account_usage_record a\n" +
            "LEFT JOIN t_finance_on_account b ON a.on_account_id = b.id\n" +
            "LEFT JOIN t_finance_bill_check c ON b.account_id = c.customer_id\n" +
            "WHERE a.is_deleted = 0 AND b.is_deleted = 0 AND b.account_id = #{account_id} AND a.currency_id = #{currency_id} AND c.approval_status = 2")
    BigDecimal sumApprovalingAmount(@Param("account_id") Long accountId,@Param("currency_id") Long currencyId);

    /**
     * 申请销账-选择挂账明细列表
     * @param req
     * @param iPage
     * @return
     */
    @Select("<script>SELECT a.*\n" +
            "FROM t_finance_on_account_usage_record a\n" +
            "LEFT JOIN t_finance_on_account b ON a.on_account_id = b.id\n" +
            "WHERE a.on_account_id = #{req.onAccountId} AND b.is_deleted = 0\n" +
            "<if test=\"req.deptId != null\">AND b.dept_id = #{req.deptId}</if> \n" +
            "<if test=\"req.createTimeBegin != null and req.createTimeEnd != null\">AND a.gmt_create BETWEEN #{req.createTimeBegin} AND #{req.createTimeEnd}</if> \n" +
            "<if test=\"req.billType != null\">AND a.bill_type = #{req.billType}</if> \n" +
            "<if test=\"req.collectionWayId != null\">AND a.collection_way_id = #{req.collectionWayId}</if> \n" +
            "<if test=\"req.productNumber != null and req.productNumber != ''\">AND a.product_number LIKE CONCAT('%',#{req.productNumber},'%') </if> \n" +
            "<if test=\"req.billNumber != null and req.billNumber != ''\">AND a.bill_number LIKE CONCAT('%',#{req.billNumber},'%') </if> \n" +
            "<if test=\"req.resourceInfo != null and req.resourceInfo != ''\">AND a.resource_info LIKE CONCAT('%',#{req.resourceInfo},'%') </if> \n" +
            "<if test=\"req.businessTypes != null \">AND a.business_type IN #{req.businessTypes}</if> \n" +
            "<if test=\"req.saleUserId != null\">AND a.sale_user_id = #{req.saleUserId}</if> \n" +
            "ORDER BY a.gmt_create DESC</script>")
    Page<OnAccountDetailListDTO> queryListByOnAccount(@Param("req") OnAccountDetailListReq req, @Param("iPage") Page<OnAccountDetailListDTO> iPage);

    /**
     * 未收金额
     * @param customerId
     */
    @Select("SELECT a.currency_id,a.currency_name,a.exchange_rate,a.total_amount\n" +
            "FROM t_finance_on_account_usage_record a\n" +
            "LEFT JOIN t_finance_on_account b ON a.on_account_id = b.id\n" +
            "WHERE b.account_id = #{account_id} AND a.is_deleted = 0\n" +
            "GROUP BY a.currency_id\n")
    List<OnAccountUsageCountDTO> selectSumByAccount(@Param("account_id") Long customerId);
}
